//
//  guanZhuShangJiaVC.m
//  Wedding fantastic
//
//  Created by ZLM on 15/6/30.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "guanZhuShangJiaVC.h"

@interface guanZhuShangJiaVC ()

@end

@implementation guanZhuShangJiaVC
-(void)dealloc{
    [_myCollectionView release];
    [_shangjiaArray release];
    [_name release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor redColor];
    self.navigationController.navigationBar.translucent = NO;
    [self creatCollectionView];
    [[ShangJiaDataBaseHandle shareDataBase]openDB];
    self.shangjiaArray = [[ShangJiaDataBaseHandle shareDataBase]selectAllshangjia];
    NSLog(@"~~~~~~~~%@",self.shangjiaArray);
    if (self.shangjiaArray.count == 0) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"您的收藏为空，快去收藏吧" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
        alert.tag = 10000;
        [self.view addSubview:alert];
        [alert show];
        
    }
}
//提示框事件
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{   //收藏为空
    if(alertView.tag == 10000){
    if(buttonIndex == 0){
        [self.navigationController popViewControllerAnimated:YES];
    }}
    //删除收藏
    if (alertView.tag == 10001) {
        if (buttonIndex == 1) {
            [[ShangJiaDataBaseHandle shareDataBase]deleteTableByShangJiaName:self.name];
            [self viewDidLoad];
        }
    }
}
- (void)creatCollectionView
{
    UICollectionViewFlowLayout *viewLayout = [[UICollectionViewFlowLayout alloc] init];
    viewLayout.itemSize = CGSizeMake((375-60)/2*kWidth/375,120*kHeight/667);
    viewLayout.sectionInset = UIEdgeInsetsMake(20*kWidth/375, 20*kHeight/667, 20*kWidth/375, 20*kHeight/667);
    self.myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,0, 375*kWidth/375, (667-49-64)*kHeight/667) collectionViewLayout:viewLayout];
    
    self.myCollectionView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.myCollectionView];
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    
    
#pragma mark --注册了，就不需要判断是否为空了
    //注册了，就不需要判断是否为空了
    [self.myCollectionView registerClass:[guanZhuShangJiaCell class] forCellWithReuseIdentifier:@"reuse"];
}
#pragma mark 实现UICollectionView的协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.shangjiaArray.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    guanZhuShangJiaCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    shangJiaModel *mod = [self.shangjiaArray objectAtIndex:indexPath.row];
    NSString *logo_path = mod.logo_path;
    
    [cell.imageV sd_setImageWithURL:[NSURL URLWithString:logo_path]];
    cell.nameLabel.text = mod.name;
    cell.fans_countLabel.text = [NSString stringWithFormat:@" 粉丝 ：%@",mod.fans_count];
    cell.fans_countLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:10];
    return cell;
    
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [[ShangJiaDataBaseHandle shareDataBase]openDB];
    guanZhuShangJiaCell *acell = (guanZhuShangJiaCell *)[collectionView cellForItemAtIndexPath:indexPath];
    self.name = acell.nameLabel.text;
    
    UIAlertView *alertVC = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"真的要删除嚒嚒" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alertVC.tag = 10001;
    
    [alertVC show];
    
}
-(void)viewWillAppear:(BOOL)animated{
    [self viewDidLoad];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
